package service

import (
	"fmt"
	"sen-golang-study/go_game_study/hero_story_go_server/game_server/internal/domain/session/entity"
	"sen-golang-study/go_game_study/hero_story_go_server/game_server/internal/domain/session/service"
)

// SessionManagementService 会话管理应用服务
type SessionManagementService struct {
	sessionDomainService *service.SessionDomainService
}

// NewSessionManagementService 创建会话管理应用服务
func NewSessionManagementService(sessionDomainService *service.SessionDomainService) *SessionManagementService {
	return &SessionManagementService{
		sessionDomainService: sessionDomainService,
	}
}

// CreateSession 创建会话
func (s *SessionManagementService) CreateSession(sessionID string, gatewayID, remoteSessionID uint32, clientIPAddr string) (*entity.Session, error) {
	return s.sessionDomainService.CreateSession(sessionID, gatewayID, remoteSessionID, clientIPAddr)
}

// GetSession 获取会话
func (s *SessionManagementService) GetSession(sessionID string) (*entity.Session, error) {
	return s.sessionDomainService.GetSession(sessionID)
}

// GetSessionByUserID 根据用户ID获取会话
func (s *SessionManagementService) GetSessionByUserID(userID uint32) (*entity.Session, error) {
	return s.sessionDomainService.GetSessionByUserID(userID)
}

// BindUserToSession 绑定用户到会话
func (s *SessionManagementService) BindUserToSession(sessionID string, userID uint32) error {
	return s.sessionDomainService.BindUserToSession(sessionID, userID)
}

// UnbindUserFromSession 从会话解绑用户
func (s *SessionManagementService) UnbindUserFromSession(sessionID string) error {
	return s.sessionDomainService.UnbindUserFromSession(sessionID)
}

// CloseSession 关闭会话
func (s *SessionManagementService) CloseSession(sessionID string) error {
	return s.sessionDomainService.CloseSession(sessionID)
}

// UpdateSessionActivity 更新会话活跃时间
func (s *SessionManagementService) UpdateSessionActivity(sessionID string) error {
	return s.sessionDomainService.UpdateSessionActivity(sessionID)
}

// CleanExpiredSessions 清理过期会话
func (s *SessionManagementService) CleanExpiredSessions(timeoutMs int64) error {
	return s.sessionDomainService.CleanExpiredSessions(timeoutMs)
}

// GetActiveSessions 获取所有活跃会话
func (s *SessionManagementService) GetActiveSessions() ([]*entity.Session, error) {
	return s.sessionDomainService.GetActiveSessions()
}

// GetSessionStats 获取会话统计信息
func (s *SessionManagementService) GetSessionStats() (map[string]interface{}, error) {
	return s.sessionDomainService.GetSessionStats()
}

// RemoveSession 移除会话
func (s *SessionManagementService) RemoveSession(sessionID string) error {
	return s.sessionDomainService.RemoveSession(sessionID)
}

// ValidateSession 验证会话有效性
func (s *SessionManagementService) ValidateSession(sessionID string) (bool, error) {
	if len(sessionID) == 0 {
		return false, fmt.Errorf("session ID cannot be empty")
	}

	session, err := s.sessionDomainService.GetSession(sessionID)
	if err != nil {
		return false, fmt.Errorf("failed to get session: %w", err)
	}

	if session == nil {
		return false, nil
	}

	return session.IsActive(), nil
}

// GetUserIDBySession 根据会话ID获取用户ID
func (s *SessionManagementService) GetUserIDBySession(sessionID string) (uint32, error) {
	if len(sessionID) == 0 {
		return 0, fmt.Errorf("session ID cannot be empty")
	}

	session, err := s.sessionDomainService.GetSession(sessionID)
	if err != nil {
		return 0, fmt.Errorf("failed to get session: %w", err)
	}

	if session == nil {
		return 0, fmt.Errorf("session not found")
	}

	return session.GetUserID(), nil
}
